<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>受保护页面 - Asurada SSO客户端</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        .protected-header {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            color: white;
            padding: 60px 0;
            margin-bottom: 40px;
        }
        .feature-card {
            transition: transform 0.3s ease;
            height: 100%;
            border: none;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .feature-card:hover {
            transform: translateY(-5px);
        }
        .security-badge {
            background: linear-gradient(135deg, #dc3545 0%, #fd7e14 100%);
            color: white;
            padding: 10px 20px;
            border-radius: 25px;
            display: inline-block;
            margin-bottom: 20px;
        }
        .user-card {
            background: linear-gradient(135deg, #6f42c1 0%, #e83e8c 100%);
            color: white;
        }
        .stats-grid {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 30px;
        }
        .stat-item {
            text-align: center;
            padding: 20px;
        }
        .stat-number {
            font-size: 2rem;
            font-weight: bold;
            color: #007bff;
        }
        .demo-section {
            background: linear-gradient(135deg, #17a2b8 0%, #6610f2 100%);
            color: white;
            border-radius: 15px;
            padding: 30px;
            margin: 30px 0;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="bi bi-shield-check"></i>
                Asurada SSO客户端
            </a>
            
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/protected">受保护页面</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/profile">个人信息</a>
                    </li>
                </ul>
                
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-person-circle"></i>
                            <span th:text="${user?.username} ?: '用户'">用户</span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="/profile">
                                <i class="bi bi-person"></i> 个人信息
                            </a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="#" onclick="logout()">
                                <i class="bi bi-box-arrow-right"></i> 登出
                            </a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <main>
        <!-- 页面头部 -->
        <section class="protected-header">
            <div class="container">
                <div class="row align-items-center">
                    <div class="col-lg-8">
                        <div class="security-badge">
                            <i class="bi bi-shield-lock me-2"></i>
                            安全认证通过
                        </div>
                        <h1 class="display-4 fw-bold mb-3">
                            受保护页面
                        </h1>
                        <p class="lead mb-4">
                            恭喜！您已成功通过SSO认证，可以访问此受保护的页面。
                            这里展示了需要登录才能查看的内容和功能。
                        </p>
                        <div class="d-flex align-items-center">
                            <i class="bi bi-check-circle-fill text-success me-2" style="font-size: 1.5rem;"></i>
                            <span class="h5 mb-0">认证状态：已验证</span>
                        </div>
                    </div>
                    <div class="col-lg-4 text-center">
                        <i class="bi bi-shield-check" style="font-size: 150px; opacity: 0.8;"></i>
                    </div>
                </div>
            </div>
        </section>

        <div class="container">
            <!-- 用户信息卡片 -->
            <div class="row mb-5">
                <div class="col-md-8">
                    <div class="card user-card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="bi bi-person-badge"></i>
                                当前登录用户
                            </h5>
                            <div class="row">
                                <div class="col-sm-6">
                                    <p class="mb-2">
                                        <strong>用户名：</strong>
                                        <span th:text="${user.username}">testuser</span>
                                    </p>
                                    <p class="mb-2">
                                        <strong>用户ID：</strong>
                                        <span th:text="${user.id}">1</span>
                                    </p>
                                </div>
                                <div class="col-sm-6">
                                    <p class="mb-2">
                                        <strong>邮箱：</strong>
                                        <span th:text="${user.email}">user@example.com</span>
                                    </p>
                                    <p class="mb-2">
                                        <strong>手机：</strong>
                                        <span th:text="${user.mobile}">13800138000</span>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body text-center">
                            <h5 class="card-title">
                                <i class="bi bi-clock-history text-primary"></i>
                                会话信息
                            </h5>
                            <p class="card-text">
                                <span class="badge bg-success mb-2">在线状态</span>
                            </p>
                            <small class="text-muted">
                                令牌有效期：30分钟<br>
                                自动刷新：支持
                            </small>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 统计信息 -->
            <div class="stats-grid mb-5">
                <h3 class="text-center mb-4">
                    <i class="bi bi-graph-up"></i>
                    系统统计信息
                </h3>
                <div class="row">
                    <div class="col-md-3">
                        <div class="stat-item">
                            <div class="stat-number" id="loginCount">1</div>
                            <div class="text-muted">今日登录次数</div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="stat-item">
                            <div class="stat-number" id="sessionTime">--</div>
                            <div class="text-muted">会话时长(分钟)</div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="stat-item">
                            <div class="stat-number text-success">100%</div>
                            <div class="text-muted">认证成功率</div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="stat-item">
                            <div class="stat-number text-info">SSO</div>
                            <div class="text-muted">认证方式</div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 功能演示区域 -->
            <div class="demo-section">
                <h3 class="mb-4">
                    <i class="bi bi-tools"></i>
                    功能演示
                </h3>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <button class="btn btn-light btn-lg w-100" onclick="checkTokenStatus()">
                            <i class="bi bi-shield-check me-2"></i>
                            检查令牌状态
                        </button>
                    </div>
                    <div class="col-md-6 mb-3">
                        <button class="btn btn-light btn-lg w-100" onclick="refreshUserInfo()">
                            <i class="bi bi-arrow-clockwise me-2"></i>
                            刷新用户信息
                        </button>
                    </div>
                    <div class="col-md-6 mb-3">
                        <button class="btn btn-light btn-lg w-100" onclick="testApiCall()">
                            <i class="bi bi-cloud-arrow-up me-2"></i>
                            测试API调用
                        </button>
                    </div>
                    <div class="col-md-6 mb-3">
                        <button class="btn btn-light btn-lg w-100" onclick="simulateTokenRefresh()">
                            <i class="bi bi-arrow-repeat me-2"></i>
                            模拟令牌刷新
                        </button>
                    </div>
                </div>
                
                <!-- 操作结果显示区域 -->
                <div id="demoResult" class="mt-4" style="display: none;">
                    <div class="alert alert-info">
                        <h6><i class="bi bi-info-circle me-2"></i>操作结果：</h6>
                        <pre id="resultContent" class="mb-0"></pre>
                    </div>
                </div>
            </div>

            <!-- 安全特性说明 -->
            <div class="row mb-5">
                <div class="col-12">
                    <h3 class="text-center mb-4">安全特性</h3>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card feature-card">
                        <div class="card-body text-center">
                            <i class="bi bi-key text-warning" style="font-size: 3rem;"></i>
                            <h5 class="card-title mt-3">JWT令牌认证</h5>
                            <p class="card-text">
                                使用JSON Web Token进行安全认证，支持访问令牌和刷新令牌机制。
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card feature-card">
                        <div class="card-body text-center">
                            <i class="bi bi-shield-lock text-success" style="font-size: 3rem;"></i>
                            <h5 class="card-title mt-3">会话保护</h5>
                            <p class="card-text">
                                自动检测令牌过期，支持无感知刷新，确保用户会话安全。
                            </p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card feature-card">
                        <div class="card-body text-center">
                            <i class="bi bi-cloud-check text-primary" style="font-size: 3rem;"></i>
                            <h5 class="card-title mt-3">分布式认证</h5>
                            <p class="card-text">
                                与SSO服务端实时同步，支持多客户端统一登录和登出。
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 快速操作 -->
            <div class="row">
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="bi bi-person text-info"></i>
                                查看个人信息
                            </h5>
                            <p class="card-text">
                                查看和管理您的详细个人信息，包括账户设置等。
                            </p>
                            <a href="/profile" class="btn btn-info">
                                <i class="bi bi-arrow-right"></i>
                                前往查看
                            </a>
                        </div>
                    </div>
                </div>
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="bi bi-house text-primary"></i>
                                返回首页
                            </h5>
                            <p class="card-text">
                                返回应用首页，查看更多功能和系统信息。
                            </p>
                            <a href="/" class="btn btn-primary">
                                <i class="bi bi-arrow-right"></i>
                                返回首页
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-light py-4 mt-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <p class="text-muted mb-0">
                        &copy; 2024 Asurada SSO客户端演示应用
                    </p>
                </div>
                <div class="col-md-6 text-end">
                    <p class="text-muted mb-0">
                        受保护页面 - 需要登录访问
                    </p>
                </div>
            </div>
        </div>
    </footer>

    <!-- JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    
    <script>
        // 页面加载时间
        const pageLoadTime = new Date();
        
        // 登出函数
        function logout() {
            if (confirm('确定要登出吗？')) {
                axios.post('/sso/logout')
                    .then(function (response) {
                        if (response.data.code === 200) {
                            window.location.href = '/';
                        } else {
                            alert('登出失败：' + response.data.message);
                        }
                    })
                    .catch(function (error) {
                        console.error('登出请求失败:', error);
                        alert('登出失败，请重试');
                    });
            }
        }
        
        // 检查令牌状态
        function checkTokenStatus() {
            showResult('正在检查令牌状态...');
            
            axios.get('/sso/check')
                .then(function (response) {
                    const result = {
                        status: response.data.code === 200 ? '成功' : '失败',
                        message: response.data.message,
                        isValid: response.data.data,
                        timestamp: new Date().toLocaleString()
                    };
                    showResult(JSON.stringify(result, null, 2));
                })
                .catch(function (error) {
                    showResult('检查失败: ' + error.message);
                });
        }
        
        // 刷新用户信息
        function refreshUserInfo() {
            showResult('正在刷新用户信息...');
            
            axios.get('/sso/userinfo')
                .then(function (response) {
                    if (response.data.code === 200) {
                        const userInfo = response.data.data;
                        const result = {
                            status: '成功',
                            userInfo: userInfo,
                            timestamp: new Date().toLocaleString()
                        };
                        showResult(JSON.stringify(result, null, 2));
                    } else {
                        showResult('刷新失败: ' + response.data.message);
                    }
                })
                .catch(function (error) {
                    showResult('刷新失败: ' + error.message);
                });
        }
        
        // 测试API调用
        function testApiCall() {
            showResult('正在测试API调用...');
            
            // 模拟一个需要认证的API调用
            axios.get('/sso/userinfo')
                .then(function (response) {
                    const result = {
                        api: '/sso/userinfo',
                        method: 'GET',
                        status: response.status,
                        success: response.data.code === 200,
                        responseTime: '< 100ms',
                        timestamp: new Date().toLocaleString()
                    };
                    showResult(JSON.stringify(result, null, 2));
                })
                .catch(function (error) {
                    const result = {
                        api: '/sso/userinfo',
                        method: 'GET',
                        error: error.message,
                        timestamp: new Date().toLocaleString()
                    };
                    showResult(JSON.stringify(result, null, 2));
                });
        }
        
        // 模拟令牌刷新
        function simulateTokenRefresh() {
            showResult('正在模拟令牌刷新...');
            
            axios.post('/sso/refresh')
                .then(function (response) {
                    if (response.data.code === 200) {
                        const result = {
                            status: '刷新成功',
                            newTokenReceived: true,
                            expiresIn: response.data.data.expiresIn + '秒',
                            timestamp: new Date().toLocaleString()
                        };
                        showResult(JSON.stringify(result, null, 2));
                    } else {
                        showResult('刷新失败: ' + response.data.message);
                    }
                })
                .catch(function (error) {
                    showResult('刷新失败: ' + error.message);
                });
        }
        
        // 显示操作结果
        function showResult(content) {
            const resultDiv = document.getElementById('demoResult');
            const resultContent = document.getElementById('resultContent');
            
            resultContent.textContent = content;
            resultDiv.style.display = 'block';
            
            // 滚动到结果区域
            resultDiv.scrollIntoView({ behavior: 'smooth' });
        }
        
        // 更新会话时长
        function updateSessionTime() {
            const now = new Date();
            const sessionMinutes = Math.floor((now - pageLoadTime) / (1000 * 60));
            document.getElementById('sessionTime').textContent = sessionMinutes;
        }
        
        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 每分钟更新一次会话时长
            updateSessionTime();
            setInterval(updateSessionTime, 60000);
            
            // 每5分钟检查一次登录状态
            setInterval(function() {
                axios.get('/sso/check')
                    .then(function (response) {
                        if (response.data.code === 200 && !response.data.data) {
                            // 如果令牌已失效，提示用户重新登录
                            if (confirm('您的登录已过期，是否重新登录？')) {
                                window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
                            }
                        }
                    })
                    .catch(function (error) {
                        console.error('检查登录状态失败:', error);
                    });
            }, 5 * 60 * 1000); // 5分钟
        });
    </script>
</body>
</html>